package yxy.game.pm2.module.godBeast;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.function;
import yxy.cherry.data.bean.therion;
import yxy.game.pm2.bean.GodBeastEquipPosition;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;


import java.util.*;
import java.util.stream.Collectors;

/**
 * 神兽槽
 */
public class PickedGodBeastModule extends BaseGameRedisLockBean {
    // bean===========================================================================
    private String playerCode;

    private HashMap<String, GodBeast> gbs = new HashMap<>(); //key:神兽type，value：神兽

    public String getPlayerCode() {
        return playerCode;
    }

    public void setPlayerCode(String playerCode) {
        this.playerCode = playerCode;
    }

    public HashMap<String, GodBeast> getGbs() {
        return gbs;
    }

    public void setGbs(HashMap<String, GodBeast> gbs) {
        this.gbs = gbs;
    }

// func===========================================================================

    public static function getData(){
        function data = DataCenter.getData(10045, function.class);
        return data;
    }

    private static PickedGodBeastModule create(String playerCode) {
        PickedGodBeastModule module = new PickedGodBeastModule();
        module.setPlayerCode(playerCode);
        //初始化4个神兽的信息
        Map<String, therion> allData = DataCenter.getAllData(therion.class);
        for (String key : allData.keySet()) {
            therion therion = allData.get(key);
            if (therion != null && therion.getLevel() == 0) {
                GodBeast godBeast = new GodBeast();
                godBeast.setCode(therion.getId());
                godBeast.setType(therion.getType());
                godBeast.setName(therion.getName());
                godBeast.setLevel(therion.getLevel());
                godBeast.setBattle(false); //默认不上阵
                godBeast.setOpen(false); //是否解锁
                godBeast.setAttack(therion.getAttack()); //攻击
                godBeast.setDefense(therion.getDefense()); //防御
                godBeast.setLife(therion.getLife()); //生命
                godBeast.setGodBeastEquips(godBeast.getGodBeastEquips());//初始装备锁定
                godBeast.setSkillLevel(1); //初始技能等级为1

                godBeast.setMaxLevel(PickedGodBeastHandle.getGodBeastMaxLevel(therion.getType()));
                godBeast.setMaxSkillLevel(PickedGodBeastHandle.getGodBeastSkillMaxLevel(therion.getType()));

                module.getGbs().put(String.valueOf(therion.getType()), godBeast);
            }
        }
        module.set();
        return module;
    }

    // redis==========================================================================
    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:GODBEASTS", playerCode);
    }

    public void set() {
        try {
            setv(key(getPlayerCode()), this);
            check();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public PickedGodBeastModule get(String playerCode) {

        try {
            PickedGodBeastModule module = getv(key(playerCode));

            //todo 后期放数据库
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), PickedGodBeastModule.class);
                if(module != null){module.set();}//把数据同步到redis
            }

            if (module == null) {
                module = create(playerCode);
            }
            module.check();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //红点
    private void check() {

    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                PickedGodBeastModule module = getv(key(playerCode));
                if (module != null) MySQLCacheDAO.setV(key, module);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    // lock==========================================================================

    static private String lockKey(String playerCode) {
        return String.format("lock:%s", key(playerCode));
    }

    static public boolean lockPickedGodBeast(String playerCode) throws InterruptedException {
        return tryLock(lockKey(playerCode));
    }

    static public void unlockPickedGodBeast(String playerCode) {
        unlock(lockKey(playerCode));
    }
}
